//package com.zxl.grocery.microservice.common.util;
//
//import java.io.*;
//import java.security.*;
//import java.security.spec.PKCS8EncodedKeySpec;
//import java.security.spec.X509EncodedKeySpec;
//
///**
// * @Author: zxl
// * @Description: 秘钥工具类
// * @Date: Created in 17:19 2017/10/12
// * @Modified By:
// */
//public class RsaKeyHelper {
//    /**
//     * 获取公钥
//     * @param filename
//     * @return
//     * @throws Exception
//     */
//    public PublicKey getPublicKey(String filename) throws Exception {
////        File f = new File(this.getClass().getClassLoader().getResource(filename).getPath());
////        FileInputStream fis = new FileInputStream(f);
////        DataInputStream dis = new DataInputStream(fis);
////        byte[] keyBytes = new byte[(int) f.length()];
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
//        DataInputStream dis = new DataInputStream(resourceAsStream);
//        byte[] keyBytes = new byte[resourceAsStream.available()];
//        dis.readFully(keyBytes);
//        dis.close();
//        X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
//        KeyFactory kf = KeyFactory.getInstance("RSA");
//        return kf.generatePublic(spec);
//    }
//
//    /**
//     * 获取密钥
//     * @param filename
//     * @return
//     * @throws Exception
//     */
//    public PrivateKey getPrivateKey(String filename) throws Exception {
////        File f = new File(this.getClass().getClassLoader().getResource(filename).getPath());
////        FileInputStream fis = new FileInputStream(f);
////        DataInputStream dis = new DataInputStream(fis);
////        byte[] keyBytes = new byte[(int) f.length()];
//        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
//        DataInputStream dis = new DataInputStream(resourceAsStream);
//        byte[] keyBytes = new byte[resourceAsStream.available()];
//        dis.readFully(keyBytes);
//        dis.close();
//        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
//        KeyFactory kf = KeyFactory.getInstance("RSA");
//        return kf.generatePrivate(spec);
//    }
//
//    /**
//     * 生成rsa公钥和密钥
//     * @param publicKeyFilename 公钥名
//     * @param privateKeyFilename 私钥名
//     * @param password 密码
//     * @throws IOException
//     * @throws NoSuchAlgorithmException
//     */
//    public void generateKey(String publicKeyFilename,String privateKeyFilename,String password) throws IOException, NoSuchAlgorithmException {
//        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
//        SecureRandom secureRandom = new SecureRandom(password.getBytes());
//        keyPairGenerator.initialize(1024, secureRandom);
//        KeyPair keyPair = keyPairGenerator.genKeyPair();
//        byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
//        FileOutputStream fos = new FileOutputStream(publicKeyFilename);
//        fos.write(publicKeyBytes);
//        fos.close();
//        byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
//        fos = new FileOutputStream(privateKeyFilename);
//        fos.write(privateKeyBytes);
//        fos.close();
//    }
//
////    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
////        generateKey("/Users/ace/git/ag-parent/ag-auth/src/main/resources/client/pub.key","/Users/ace/git/ag-parent/ag-auth/src/main/resources/client/pri.key","1*&623!f");
////    }
//}
//
